package com.database.controller;

import com.database.character.Attention;
import com.database.character.Result;
import com.database.service.AttentionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/attentions")
@Api(value = "关注controller",tags = {"关注相关接口"})
public class AttentionController {
	@Autowired
	private AttentionService attentionService;
	/*添加关注*/
	@PostMapping
	@ApiOperation(value = "添加关注",notes = "添加关注",httpMethod = "POST")
	@ApiResponse(code = 200, message = "添加关注成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,dataTypeClass = String.class,paramType = "header")
	})
	public Result addAttention(@RequestBody Attention attention){
		Integer userId = attention.getUserId();
		Integer focusedId = attention.getFocusedId();
		int i = attentionService.addAttention(attention);
		if(i==0){
			return Result.error("关注失败！");
		}
		else{
			Attention byUidAndFid = attentionService.getByUidAndFid(userId, focusedId);
			return Result.success("关注成功！",byUidAndFid);
		}
	}
	/*打开关注列表*/
	@GetMapping("/{userId}")
	@ApiOperation(value = "打开关注列表",notes = "打开关注列表",httpMethod = "GET")
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getAttentionByID(@PathVariable Integer userId ){
		List<Attention> attentionByUid = attentionService.getAttentionByUid(userId);
		return Result.success("查询成功！",attentionByUid);
	}
	/*取消关注*/
	@DeleteMapping("/{attentionId}")
	@ApiOperation(value = "取消关注",notes = "取消关注",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "删除成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "attentionId",value = "关注id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteAttention(@PathVariable Integer attentionId){
		int i = attentionService.deleteByAttentionId(attentionId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}

	@DeleteMapping("/{userId}/{focusedId}")
	@ApiOperation(value = "取消关注",notes = "取消关注",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "删除成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "focusedId",value = "被关注用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteAttentionByUidAndFid(@PathVariable Integer userId,@PathVariable Integer focusedId){
		int i = attentionService.deleteByUidAndFid(userId, focusedId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}

	@GetMapping("/{userId}/{focusedId}")
	@ApiOperation(value = "查询关注",notes = "查询关注",httpMethod = "GET")
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "focusedId",value = "被关注用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getByUidAndFid(@PathVariable Integer userId,@PathVariable Integer focusedId){
		Attention byUidAndFid = attentionService.getByUidAndFid(userId, focusedId);
		return Result.success("查询成功！",byUidAndFid);
	}

}
